<template>
  <div class="container">
    <div class="container__left">
      <div style="width: 260px; height: 36px">
        <el-input placeholder="输入部门名称进行过滤" v-model="filterText">
        </el-input>
      </div>
      <el-tree
        class="filter-tree"
        :data="organizationData"
        :props="treeProps"
        default-expand-all
        :filter-node-method="filterNode"
        ref="tree"
        node-key="id"
        :expand-on-click-node="false"
        accordion
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span>
            <el-button
              type="text"
              size="mini"
              @click="() => openDeptModel(1, data)"
            >
              新建下级
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="() => openDeptModel(2, data)"
            >
              查看
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="() => openDeptModel(3, data)"
            >
              编辑
            </el-button>
            <!-- @confirm="delWarnLevel(scope.row)" -->
            <el-popconfirm
              title="确认删除此部门信息吗？"
              @confirm="remove(data)"
            >
              <el-button
                v-if="data.pid != 0"
                type="text"
                size="mini"
                slot="reference"
              >
                删除
              </el-button>
            </el-popconfirm>
          </span>
        </span>
      </el-tree>
    </div>

    <div class="container__right">
      <div class="title">
        <span>部门列表</span>
      </div>
      <!-- <div class="align-center">
        <div style="width: 260px; height: 36px">
          <el-input placeholder="输入部门名称过滤"> </el-input>
        </div>
        <div class="add-btn">
          <span><i class="el-icon-plus" @click="addDept()" />新建部门</span>
        </div>
      </div> -->
      <el-table
        max-height="734"
        :data="tableData"
        style="
          width: 100%;
          margin-top: 30px;
          background-color: #303136;
          color: #ffff;
        "
        :border="false"
      >
        <el-table-column prop="sort" label="序号"> </el-table-column>
        <el-table-column prop="name" label="部门名称"> </el-table-column>
        <el-table-column prop="duty" label="部门负责人"> </el-table-column>
        <el-table-column prop="mobile" label="负责人电话"> </el-table-column>
        <el-table-column prop="isStop" label="部门状态">
          <template slot-scope="scope">
            <span style="color: #3ad76e" v-if="scope.row.isStop == 1"
              >启用</span
            >
            <span style="color: #e6873a" v-else>关闭</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="opration" label="操作">
          <template slot-scope="scope">
            <span class="opration-btn" @click="handleClick(scope.row)"
              >编辑</span
            >
            <span class="opration-btn" @click="handleClick(scope.row)"
              >删除</span
            >
          </template>
        </el-table-column> -->
      </el-table>
      <!-- <div class="table-pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="4"
          :page-sizes="[20, 50, 60, 100]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="400"
        >
        </el-pagination>
      </div> -->
    </div>

    <el-dialog
      :modal-append-to-body="false"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="35%"
    >
      <dept-setting
        @onClose="closeModel"
        v-if="dialogVisible"
        :modeType="modeType"
        :organizationInfo="currentOrganizetionData"
      ></dept-setting>
    </el-dialog>
  </div>
</template>

<script>
import { getDeptAll, getDeptList, addDept, delDept } from "@/api/dept";
import DeptSetting from "./components/DeptSetting.vue";
export default {
  components: {
    DeptSetting,
  },
  data() {
    return {
      filterText: "",
      organizationData: [], //JSON.parse(JSON.stringify(organizationData)),
      treeProps: {
        children: "children",
        label: "name",
      },
      tableData: [],
      dialogVisible: false,
      dialogTitle: "新增部门",
      currentOrganizetionData: {},
      modeType: 0,
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  mounted() {
    this.getDeptAllList();
    this.getDeptLists();
  },
  methods: {
    closeModel(e) {
      this.dialogVisible = false;
      if (e.isClose) {
        this.organizationData = [];
        this.tableData = [];
        this.getDeptLists();
        this.getDeptAllList();
      }
    },
    //部门列表
    async getDeptLists() {
      let result = await getDeptList({ isStop: "", name: "" });
      if (result.code == 200) {
        this.organizationData = result.data && result.data;
      }
    },

    //查询所有部门
    async getDeptAllList() {
      let result = await getDeptAll();
      if (result.code == 200) {
        this.tableData = result.data;
      }
    },

    //openDeptModel
    openDeptModel(type, data) {
      this.dialogVisible = true;
      this.modeType = type;
      this.currentOrganizetionData = data;
      if (type == 1) {
        //add
        this.dialogTitle = "新增部门";
      } else if (type == 2) {
        //see
        this.dialogTitle = "部门信息";
      } else {
        //edit
        this.dialogTitle = "编辑部门";
      }
    },

    //新建部门
    async addDepts(data) {
      console.log(data, "xinz");
      let result = await addDept({
        isStop: 1,
        pid: 5,
        name: "默认部门子级大佬积分啦的",
        // mobile: "",d
        duty: "3333",
        sort: 1,
      });
      console.log(result, "新建部门");
    },

    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },

    async remove(data) {
      let res = await delDept({ id: data.id });
      if (res.code == 200) {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.organizationData = [];
        this.tableData = [];
        this.getDeptAllList();
        this.getDeptLists();
      }
    },

    handleClick() {},
  },
};
</script>

<style lang="scss" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
  background: transparent;
  color: #fff;
}

.container {
  display: flex;

  &__left {
    width: 532px;
    height: 100%;
    border-right: 1px solid #3b3e42;
  }

  &__right {
    width: calc(100% - 532px);
    padding: 16px;
    box-sizing: border-box;

    .title {
      text-align: left;
      color: #fff;
      padding: 24px 16px;
      font-size: 18px;
    }

    .add-btn {
      width: 112px;
      height: 36px;
      text-align: center;
      line-height: 36px;
      color: #17181a;
      background: #5cfbff;
      border-radius: 4px;
      margin-left: 16px;

      cursor: pointer;
    }

    // .table-pagination {
    //   text-align: right;
    //   margin: 30px 0;
    // }

    .opration-btn {
      display: inline-block;
      width: 52px;
      height: 32px;
      text-align: center;
      line-height: 32px;
      color: #fff;
      background: #40414d;
      border-radius: 4px;
      cursor: pointer;
      margin-right: 10px;
    }
  }
}
</style>

<style>
.el-tree {
  background: transparent !important;
  color: #fff;
  margin-top: 24px;
}

.el-button--text {
  color: #5cfbff;
  font-size: 14px;
}

.el-tree-node:focus > .el-tree-node__content {
  background-color: #363940 !important;
}
.el-tree-node__content:hover {
  background-color: #363940;
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  background-color: #363940;
}
</style>